package avicit.bdp.dms.tdm.job;

import avicit.bdp.common.dto.BdpPrmEngineResourceDTO;
import avicit.bdp.common.dto.StorageResourceConf;
import avicit.bdp.common.utils.CheckParaUtils;
import avicit.bdp.common.utils.uploads.FileAdapterUtils;
import avicit.bdp.common.utils.uploads.IFileAdapter;
import avicit.bdp.core.file.util.FileUtils;
import avicit.bdp.dms.prm.service.BdpPrmEngineResourceService;
import avicit.bdp.dms.tdm.dto.BdpAssetDownloadDTO;
import avicit.bdp.dms.tdm.service.BdpAssetDownloadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class BdpAssetJob {
    @Autowired
    private BdpAssetDownloadService assetDownloadService;
    @Autowired
    private BdpPrmEngineResourceService engineResourceService;

    @Scheduled(cron = "0 0 0 * * ?")
    public void deleteCsvFile() throws Exception {
        BdpPrmEngineResourceDTO engineResourceDTO =
                engineResourceService.findDefaultEngineResource(1);
        StorageResourceConf conf = engineResourceDTO.getStorageResourceConf();
        IFileAdapter fileAdapter = FileAdapterUtils.getFileService(conf);
        List<BdpAssetDownloadDTO> assetDownLoads = assetDownloadService.selectListAll();
        for (BdpAssetDownloadDTO assetDownLoad : assetDownLoads) {
            String filePath = assetDownLoad.getFilePath();
            fileAdapter.delete(filePath,true);
            assetDownloadService.deleteByPrimaryKey(assetDownLoad.getId());
            FileUtils.deleteTempFile(filePath);
        }
    }
}
